package com.huixin.powermonitor.controller;


import cn.hutool.db.AbstractDb;
import cn.smallbun.screw.core.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huixin.powermonitor.common.QueryPageParam;
import com.huixin.powermonitor.common.Result;
import com.huixin.powermonitor.entity.AlarmRecords;
import com.huixin.powermonitor.entity.Device;
import com.huixin.powermonitor.entity.User;
import com.huixin.powermonitor.service.AlarmRecordsService;
import com.huixin.powermonitor.service.DeviceService;
import com.huixin.powermonitor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huixin
 * @since 2025-07-08
 */
//@CrossOrigin(originPatterns = "*",maxAge = 3600)
@RestController
@RequestMapping("/alarm-records")
public class AlarmRecordsController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private AlarmRecordsService alarmRecordsService;
    @Autowired
    private UserService userService;


    @PostMapping("/listPage")
    public Result alarm(@RequestBody QueryPageParam params)  {

        int pageNum = params.getPageNum();
        int pageSize = params.getPageSize();

        Page<AlarmRecords> page = new Page<>(pageNum, pageSize);
        HashMap<String,Object> map = params.getParam();
        LambdaQueryWrapper<AlarmRecords> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Device> deviceLambdaQueryWrapper= new LambdaQueryWrapper<>();
        if( map.containsKey("deviceName") && StringUtils.isNotBlank(map.get("deviceName").toString()))  {
            String deviceName = map.get("deviceName").toString();
            deviceLambdaQueryWrapper.like(Device::getDeviceName,deviceName);
            List<Device> deviceList=deviceService.list(deviceLambdaQueryWrapper);
            for(Device d:deviceList){
                queryWrapper.like(AlarmRecords::getDeviceId,d.getDeviceId());
            }
        }
        IPage<AlarmRecords> AlarmRecordsDataPage = alarmRecordsService.page(page, queryWrapper);
        List<AlarmRecords> records = AlarmRecordsDataPage.getRecords();
        for(AlarmRecords record : records) {
            Device device = deviceService.getById(record.getDeviceId());
            if (device != null) {
                record.setDeviceName(device.getDeviceName());
            } else {
                record.setDeviceName("未知设备");
            }
        }
        return Result.suc(AlarmRecordsDataPage.getTotal(),records);
    }

    @PostMapping("/addData")
    public Result addAlarmRecords(@RequestBody AlarmRecords alarmRecords){
        alarmRecordsService.save(alarmRecords);
        return Result.suc();
    }

    @PostMapping("/delete")
    public Result deleteAlarmRecords(@RequestBody Long[] ids){
        alarmRecordsService.removeByIds(Arrays.asList(ids));
        return Result.suc();
    }
    @PostMapping("listPage/solved")
    public Result alarmSolved(@RequestBody QueryPageParam params)  {

        int pageNum = params.getPageNum();
        int pageSize = params.getPageSize();
        Page<AlarmRecords> page = new Page<>(pageNum, pageSize);
        HashMap<String,Object> map = params.getParam();
        LambdaQueryWrapper<AlarmRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlarmRecords::getProcessStatus,"已处理");
        LambdaQueryWrapper<Device> deviceLambdaQueryWrapper= new LambdaQueryWrapper<>();
        if( map.containsKey("deviceName") && StringUtils.isNotBlank(map.get("deviceName").toString()))  {
            String deviceName = map.get("deviceName").toString();
            deviceLambdaQueryWrapper.like(Device::getDeviceName,deviceName);
            List<Device> deviceList=deviceService.list(deviceLambdaQueryWrapper);
            for(Device d:deviceList){
                queryWrapper.like(AlarmRecords::getDeviceId,d.getDeviceId());
            }
        }

        if( map.containsKey("device_id") && StringUtils.isNotBlank(map.get("device_id").toString()))  {
            String deviceId = map.get("device_id").toString();
            queryWrapper.eq(AlarmRecords::getDeviceId,Integer.parseInt(deviceId));
            System.out.println("deviceId"+Integer.parseInt(deviceId));
        }
        IPage<AlarmRecords> AlarmRecordsDataPage = alarmRecordsService.page(page, queryWrapper);
        List<AlarmRecords> records = AlarmRecordsDataPage.getRecords();
        for(AlarmRecords record : records) {
            Device device = deviceService.getById(record.getDeviceId());
            User user = userService.getById(record.getProcessUserId());
            if (device != null) {
                record.setDeviceName(device.getDeviceName());
            } else {
                record.setDeviceName("未知设备");
            }

            if (user != null) {
                record.setName(user.getName());
            } else {
                record.setName("未知设备");
            }
        }
        return Result.suc(AlarmRecordsDataPage.getTotal(),records);
    }

    @PostMapping("listPage/unsolved")
    public Result alarmUnsolved(@RequestBody QueryPageParam params)  {

        int pageNum = params.getPageNum();
        int pageSize = params.getPageSize();

        Page<AlarmRecords> page = new Page<>(pageNum, pageSize);
        HashMap<String,Object> map = params.getParam();
        LambdaQueryWrapper<AlarmRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlarmRecords::getProcessStatus,"未处理");
        LambdaQueryWrapper<Device> deviceLambdaQueryWrapper= new LambdaQueryWrapper<>();
        if( map.containsKey("deviceName") && StringUtils.isNotBlank(map.get("deviceName").toString()))  {
            String deviceName = map.get("deviceName").toString();
            deviceLambdaQueryWrapper.like(Device::getDeviceName,deviceName);
            List<Device> deviceList=deviceService.list(deviceLambdaQueryWrapper);
            for(Device d:deviceList){
                queryWrapper.like(AlarmRecords::getDeviceId,d.getDeviceId());
            }
        }
        IPage<AlarmRecords> AlarmRecordsDataPage = alarmRecordsService.page(page, queryWrapper);
        List<AlarmRecords> records = AlarmRecordsDataPage.getRecords();
        for(AlarmRecords record : records) {
            Device device = deviceService.getById(record.getDeviceId());
            if (device != null) {
                record.setDeviceName(device.getDeviceName());
            } else {
                record.setDeviceName("未知设备");
            }
        }
        return Result.suc(AlarmRecordsDataPage.getTotal(),records);
    }
    @PostMapping("update/{id}")
    public Result updateAlarmRecords(@PathVariable("id") Long id){
        AlarmRecords alarmRecords = alarmRecordsService.getById(id);
        alarmRecords.setProcessStatus("已处理");
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getName,UserController.user1.getName());
        User user = userService.getOne(userLambdaQueryWrapper);
        alarmRecords.setProcessUserId(user.getId());
        alarmRecords.setProcessTime(LocalDateTime.now());
        alarmRecordsService.updateById(alarmRecords);

        return Result.suc();
    }
}
